﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class GenderData
    {
        public Gender GetGender(int genderId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var gender = ctx.Genders.Where(d => d.GenderID == genderId);
                return gender.FirstOrDefault();
            }
        }

        public List<Gender> GetAllGenders()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Genders.ToList();
            }
        }

        public int AddNewGender(Gender gender)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToGenders(gender);
                ctx.SaveChanges();

                Gender newGender = ctx.Genders.Where(
                    d => d.Name == gender.Name).FirstOrDefault();

                if (newGender == null)
                    return -1;
                else
                    return newGender.GenderID;
            }
        }

        public void UpdateGender(Gender gender)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Gender dbDept = ctx.Genders.First(d => d.GenderID == gender.GenderID);
                ctx.ApplyCurrentValues("Genders", gender);
                ctx.SaveChanges();
            }
        }

        public void DeleteGender(int genderId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Gender dbDept = ctx.Genders.First(d => d.GenderID == genderId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }

    }
}
